<template>
  <div>
    <div>
      <van-nav-bar
        title="登录"
        left-text="返回"
        right-text="注册"
        left-arrow
        @click-left="onClickLeft"
        @click-right="onClickRight"
      />
    </div>
    <div class="img">
      <van-image
        round
        width="10rem"
        fit="contain"
        height="10rem"
        src="https://img01.yzcdn.cn/vant/cat.jpeg"
      />
    </div>
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="username"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
          >提交</van-button
        >
      </div>
    </van-form>
  </div>
</template>
<script>
import Vue from 'vue'
import { Form, Field, Image as VanImage, NavBar, Toast } from 'vant'

Vue.use(Form)
Vue.use(VanImage)
Vue.use(Field)
Vue.use(NavBar)
Vue.use(Toast)
export default {
  created() {
    this.$store.commit('hide')
  },
  data() {
    return {
      username: '',
      password: ''
    }
  },
  beforeDestroy() {
    this.$store.commit('chuxian')
  },
  methods: {
    onSubmit(values) {
      // console.log('submit', values)
      let str = ''
      let f = ''
      for (let key in values) {
        str += f + key + '=' + values[key]
        f = '&'
      }
      this.$http.post('/users/login', str).then((res) => {
        if (res.code === 1) {
          Toast('登录成功')
          this.$router.push('/my')
        } else {
          Toast('登录失败，检查账号密码')
        }
      })
    },
    onClickLeft() {
      this.$router.go(-1)
    },
    onClickRight() {
      Toast('暂时注册不了 您去别家看看')
    }
  }
}
</script>
<style scoped lang='scss'>
.van-image {
  padding: 50px 0;
}
.img {
  width: 100%;
  text-align: center;
}
</style>
